package com.apes.scm.stock.inventory.model;


import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import com.apes.scm.stock.inventory.repository.InventoryResolveRecordRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 盘点差异复盘
 * @author yiny
 * @create 2018.9.28
 *
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = InventoryDiffCompound.CONST_TABLE_NAME)
public class InventoryDiffCompound extends BaseModel{
    public static final String CONST_TABLE_NAME = "sto_inventory_diff_Compound";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, padding = 1, name = "盘点差异复盘")
    private String id; // 唯一编码

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "inventory_ids", referencedColumnName = "id")
    private InventoryList inventoryListIds; // 来源编码

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id", nullable = false)
    private Product productId; // 商品编码

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "unit_id", referencedColumnName = "id")
    private ProductUom unitId; // 计量单位

    @Column(name = "custody_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double custodyQty = 0; // 保管数

    @Column(name = "intransit_qty", columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double inTransitQty = 0; // 在途(领用在途 + 转储发货在途)

    @Column(name = "first_set_qty", columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double firstSetQty = 0; // 初盘数量

    @Column(name = "initial_discrepancies_qty", columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double initialDiscrepanciesQty = 0; // 初盘差异

    @Column(name = "compound_qty", columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double compoundQty = 0; // 复盘数量

    @Column(name = "complex_difference_qty", columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double complexDifferenceQty = 0; // 复盘差异

    @Column(name = "processed_qty", columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double processedQty = 0; // 已处理数量

    public void addProcessedQty(double qty) {
        this.processedQty = this.processedQty + qty;
    }

    public String acquireProductCode() {
        return productId.getId();
    }



    @Transient
    List<InventoryResolveRecord> inventoryResolveRecordList = new ArrayList<>();
    public List<InventoryResolveRecord> getInventoryResolveRecordList() {
        if ( this.getInventoryListIds().getState() == null || !this.getInventoryListIds().getState().equals("review")) return new ArrayList<>();

        return SpringManager.getBean(InventoryResolveRecordRepository.class).findAll(
                JpaDsl.toCriteriaByEq("InventoryDiffCompound.id", this.id)
        );
    }

}






















